<?php
/*
 * @Descripttion: 
 * @version: 
 * @Author: Lk
 * @Date: 2023-06-21 09:31:26
 * @LastEditors: Lk
 * @LastEditTime: 2023-07-07 13:42:05
 */

/**
 * hook钩子触发短信发送
 */

namespace app\common\hook;

//阿里云短信发送相关类 composer安装 
//composer require alibabacloud/sdk
use Swoft\Task\Bean\Annotation\Task;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use think\Exception;

class Sms
{

    //发送验证码
    //@sms  验证码发送记录数数据库实例
    //$sms->mobile 手机号码
    //['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]
    function smsSend($sms)
    {
        return $this->sendMsg($sms->mobile, ['code' => $sms->code], 'code');
    }

    function smsCheck($sms)
    {	
    	return $this->sendMsg($sms->mobile,['code'=>$sms->code],'code');
    }


    //@mobile 手机号码
    //@param  短信发送参数
    //@template 短信发送模板编号key值
    private function sendMsg($mobile, $param, $template)
    {
        $config = \config('alisms');
        if (empty($param) || !isset($config) || empty($config)) {
            return false;
        }
        AlibabaCloud::accessKeyClient($config['accessKeyId'], $config['accessSecret'])
            ->regionId($config['regionId'])
            ->asGlobalClient();
        $TemplateCode = isset($config['TemplateCode'][$template]) ? $config['TemplateCode'][$template] : $config['TemplateCode']['code'];
        try {
            $result = AlibabaCloud::rpcRequest()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'PhoneNumbers' => $mobile,
                        'SignName' => $config['SignName'],
                        'TemplateCode' => $TemplateCode,
                        'TemplateParam' => json_encode($param)
                    ],
                ])
                ->request();
            if ($result->Code == "OK") {
                return true;
            } else {
                exception($result->Message);
            }
        } catch (Exception $e) {
            return $e->getMessage();
        } catch (ClientException $e) {
            return $e->getErrorMessage();
        } catch (ServerException $e) {
            return $e->getErrorMessage();
        }
    }
}
